package com.example.project3.model;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 书籍模型类
 * 包含书籍的基本信息：ID、书号、书名、作者、价格等
 */
public class Book implements Parcelable {
    private int id;
    private String bookNumber;  // 书号
    private String bookName;    // 书名
    private String author;      // 作者
    private double price;       // 价格
    private String userEmail;   // 所属用户邮箱

    // 无参构造函数
    public Book() {
    }

    // 全参构造函数
    public Book(int id, String bookNumber, String bookName, String author, double price, String userEmail) {
        this.id = id;
        this.bookNumber = bookNumber;
        this.bookName = bookName;
        this.author = author;
        this.price = price;
        this.userEmail = userEmail;
    }

    // 不含ID的构造函数（用于新增书籍）
    public Book(String bookNumber, String bookName, String author, double price, String userEmail) {
        this.bookNumber = bookNumber;
        this.bookName = bookName;
        this.author = author;
        this.price = price;
        this.userEmail = userEmail;
    }

    // Getter 和 Setter 方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBookNumber() {
        return bookNumber;
    }

    public void setBookNumber(String bookNumber) {
        this.bookNumber = bookNumber;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    // Parcelable 实现，用于在Activity间传递对象
    protected Book(Parcel in) {
        id = in.readInt();
        bookNumber = in.readString();
        bookName = in.readString();
        author = in.readString();
        price = in.readDouble();
        userEmail = in.readString();
    }

    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(bookNumber);
        dest.writeString(bookName);
        dest.writeString(author);
        dest.writeDouble(price);
        dest.writeString(userEmail);
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookNumber='" + bookNumber + '\'' +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", userEmail='" + userEmail + '\'' +
                '}';
    }
}